from django.contrib.auth import logout
from django.http import JsonResponse
from django.shortcuts import render
from rest_framework.generics import CreateAPIView
from rest_framework.response import Response

from .models import User
from .serializers import CreateUserSerializer
from rest_framework.views import APIView
# Create your views here.


class UserView(CreateAPIView):
    # 用户注册
    # 指定序列化器
    serializer_class = CreateUserSerializer


class UsernameCountView(APIView):
    # 判断用户名是否已注册
    def get(self, request, username):
        # 查询user表
        count = User.objects.filter(username=username).count()

        # 包装响应数据
        data = {
            'username': username,
            'count': count
        }

        # 响应
        return Response(data)


class MobileCountView(APIView):
    # 判断手机号是否已注册
    def get(self, request, mobile):
        # 查询user表
        count = User.objects.filter(mobile=mobile).count()

        # 包装响应数据
        data = {
            'mobile': mobile,
            'count': count
        }

        # 响应
        return Response(data)


class LogoutView(APIView):
    # 用户退出登录
    def delete(self, request):

        return Response({'code': '0', 'errmsg': 'ok'})
        # # 包装响应数据
        # data = {
        #     'mobile': mobile,
        #     'count': count
        # }
        #
        # # 响应
        # return Response(data)